package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.ProxyGroupRateDTO;
import com.ayf.payment.game.api.dto.criteria.ProxyGroupCriteria;
import com.ayf.payment.game.api.dto.platform.PaymentProductAndRateDTO;
import com.ayf.payment.game.api.entity.ProxyGroup;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.ProxyGroupMapper;
import com.ayf.payment.game.api.mapper.ProxyGroupRateMapper;
import com.ayf.payment.game.api.service.ProxyGroupService;
import com.ayf.payment.game.api.service.platform.PaymentProductService;
import com.ayf.payment.game.api.type.YesAndNo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ProxyGroupServiceImpl implements ProxyGroupService {

    @Resource
    private ProxyGroupMapper proxyGroupMapper;

    @Resource
    private ProxyGroupRateMapper proxyGroupRateMapper;

    @Resource
    private PaymentProductService paymentProductService;

    @Resource
    private com.ayf.payment.game.api.config.SystemInitialization systemInitialization;

    @Override
    public boolean insert(ProxyGroupCriteria proxyGroupCriteria) {
        if (proxyGroupCriteria == null) {
            throw new ParameterException("分组信息不能为空");
        }
        if (proxyGroupCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        if (StringUtils.isBlank(proxyGroupCriteria.getGroupName())) {
            throw new ParameterException("分组名称不能为空");
        }
        ProxyGroup proxyGroup = new ProxyGroup();
        proxyGroup.setMerchantId(proxyGroupCriteria.getMerchantId());
        proxyGroup.setGroupName(proxyGroupCriteria.getGroupName());
        ProxyGroup db = proxyGroupMapper.selectDefaultsGroup(proxyGroupCriteria.getMerchantId());
        if (db == null) {
            proxyGroup.setDefaults(YesAndNo.YES);
        }
        if (proxyGroupMapper.insertSelective(proxyGroup) > 0) {
            for(ProxyGroupRateDTO pgr:proxyGroupCriteria.getGroupRateList()){
                PaymentProductAndRateDTO prdto= paymentProductService.getPaymentMerchantProductAndRateDTO(proxyGroup.getMerchantId(),pgr.getPayProductId());
                if(pgr.getPaymentRate().compareTo(new BigDecimal(prdto.getPaymentRate()))==1) {
                    throw new ParameterException("费率不能高于代理商户费率("+prdto.getPaymentRate()+")！");
                }
            }
            int num=proxyGroupRateMapper.insertRates(proxyGroup.getId(), proxyGroupCriteria.getGroupRateList());
            systemInitialization.InitializationData("添加用户分组");
            return num > 0;
        }

        return false;
    }

    @Override
    public List<ProxyGroup> selectGroup(Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("商户id不能为空");
        }
        return proxyGroupMapper.selectGroup(merchantId);
    }

    @Override
    public ProxyGroupCriteria selectGroupInfo(ProxyGroupCriteria proxyGroupCriteria) {
        if (proxyGroupCriteria == null) {
            throw new ParameterException("分组信息不能为空");
        }
        if (proxyGroupCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        if (proxyGroupCriteria.getGroupId() == null) {
            throw new ParameterException("分组id不能为空");
        }
        return proxyGroupMapper.selectGroupInfo(proxyGroupCriteria);
    }

    @Override
    public List<ProxyGroup> selectAllGroupInfo() {
        return proxyGroupMapper.selectAllGroupInfo();
    }

    @Override
    public boolean updateGroup(ProxyGroupCriteria proxyGroupCriteria) {
        if (proxyGroupCriteria == null) {
            throw new ParameterException("分组信息不能为空");
        }
        if (proxyGroupCriteria.getGroupId() == null) {
            throw new ParameterException("分组id不能为空");
        }
        if (StringUtils.isBlank(proxyGroupCriteria.getGroupName())) {
            throw new ParameterException("分组名称不能为空");
        }
        ProxyGroup proxyGroup = new ProxyGroup();
        proxyGroup.setGroupName(proxyGroupCriteria.getGroupName());
        proxyGroup.setId(proxyGroupCriteria.getGroupId());
        if (proxyGroupMapper.updateByPrimaryKeySelective(proxyGroup) > 0) {
            proxyGroupRateMapper.deleteRates(proxyGroupCriteria.getGroupId());
            for(ProxyGroupRateDTO pgr:proxyGroupCriteria.getGroupRateList()){
                PaymentProductAndRateDTO prdto= paymentProductService.getPaymentMerchantProductAndRateDTO(proxyGroupCriteria.getMerchantId(),pgr.getPayProductId());
                if(pgr.getPaymentRate().compareTo(new BigDecimal(prdto.getPaymentRate()))==1) {
                    throw new ParameterException("费率不能高于代理商户费率("+prdto.getPaymentRate()+")！");
                }
            }
            int num =proxyGroupRateMapper.insertRates(proxyGroupCriteria.getGroupId(),
                    proxyGroupCriteria.getGroupRateList());
            systemInitialization.InitializationData("更新用户分组");
            return  num> 0;
        }

        return false;
    }

    @Override
    public boolean updateGroupDefaults(ProxyGroupCriteria proxyGroupCriteria) {
        if (proxyGroupCriteria == null) {
            throw new ParameterException("分组信息不能为空");
        }
        if (proxyGroupCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        if (proxyGroupCriteria.getGroupId() == null) {
            throw new ParameterException("分组id不能为空");
        }
        proxyGroupMapper.updateGroupDefaults(proxyGroupCriteria.getMerchantId());
        ProxyGroup proxyGroup = new ProxyGroup();
        proxyGroup.setId(proxyGroupCriteria.getGroupId());
        proxyGroup.setDefaults(YesAndNo.YES);
        return proxyGroupMapper.updateByPrimaryKeySelective(proxyGroup) > 0;
    }

    @Override
    public ProxyGroup selectById(Integer groupId) {
        return proxyGroupMapper.selectByPrimaryKey(groupId);
    }

    @Override
    public boolean deleteGroup(ProxyGroupCriteria proxyGroupCriteria) {
        if (proxyGroupCriteria == null) {
            throw new ParameterException("分组信息不能为空");
        }
        if (proxyGroupCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        if (proxyGroupCriteria.getGroupId() == null) {
            throw new ParameterException("分组id不能为空");
        }
        ProxyGroup proxyGroup = new ProxyGroup();
        proxyGroup.setMerchantId(proxyGroupCriteria.getMerchantId());
        proxyGroup.setId(proxyGroupCriteria.getGroupId());
        ProxyGroup dbProxyGroup = proxyGroupMapper.selectOne(proxyGroup);
        if (dbProxyGroup == null) {
            return false;
        }
        if (dbProxyGroup.getMerchantNum() > 0) {
            throw new ParameterException("分组下商户数量大于0，不能删除");
        }
        if ("YES".equals(dbProxyGroup.getDefaults().name())) {
            throw new ParameterException("默认分组不能删除");
        }
        proxyGroupMapper.delete(proxyGroup);
        return true;
    }
}



